<?php

namespace Shop\Controller;
use Think\Server;

header('content-type:text/html;charset=utf-8');
class BaseController extends Server {

	protected $socket = 'websocket://0.0.0.0:7777';
	public $apihost = "http://127.0.0.1/";

	/*添加定时器
	 *监控连接状态
	 * */
	public function onWorkerStart(){
		
		//PC蛋蛋北京28采集
		if (C('bj28_kj_select') == 1) {
			//xyft开奖采集
			\Workerman\Lib\Timer::add(60, function(){
				$time = time();
					$url = $this->apihost . "xy28sys/5fc.php";
					$josn2 = http_get($url);
					$fdata = json_decode($josn2,true);
					//$fdata = json_decode($josn2,true);

					$fdata = $fdata['data'][0];
					//$cr_data = json_decode($cpk,true);
					
					//foreach ($fdata  as  $v) {
						$periodnumber = $fdata['expect'];
						$awardtime = date('Y-m-d H:i:s', strtotime($fdata['opentime']));
						$awardnumbers = $fdata['opencode'];
/*						
$file = fopen("caijiaaa.txt","w");
echo fwrite($file,'$fdata:'.var_export($fdata,true));
fclose($file);*/
$file = fopen("caijiaaa.txt","w");
echo fwrite($file,'$fdata:'.$periodnumber.'-'.$awardtime.'-'.$awardnumbers);
fclose($file);

						
						if ($periodnumber && $awardnumbers && $awardtime) {
							$data = array(
								'periodnumber' => $periodnumber,
								'awardnumbers' => $awardnumbers,
								'awardtime' => $awardtime,
								'game' => 'bj28',
								'addtime' => time()
							);
							$caijinum = M('caiji')->where("game = 'bj28'")->limit(0,1)->order("id desc")->find();
							if (strval($caijinum['periodnumber']) != $periodnumber  && $periodnumber > $caijinum['periodnumber']) {
								//控制开奖
								$kongzhi = M('kongzhi')->where("periodnumber = {$periodnumber} and game='bj28'")->limit(0,1)->order("id desc")->find();
								if ($kongzhi) {
									$data['awardnumbers'] = $kongzhi['awardnumbers'];
								}												
								M('caiji')->add($data);
							}
						}
					//}
				
					
	    	});
		} else {
			\Workerman\Lib\Timer::add(60, function(){
				$time = time();
					$url = C('bj28_api');
					$cpk = http_get($url);
					$cr_data = json_decode($cpk);
					foreach ($cr_data as $key => $value) {
						$periodnumber = $key;
						$awardtime = $value->dateline;

						$number = explode(',',$value->number);
						$one = array_sum(array_slice($number,0,6));
						$two = array_sum(array_slice($number,6,6));
						$three = array_sum(array_slice($number,12,6));
						
						$one_end = substr($one,-1);
						$two_end = substr($two,-1);
						$three_end = substr($three,-1);

						$awardnumbers = "$one_end,$two_end,$three_end";
						break;
					}

					if ($periodnumber && $awardnumbers && $awardtime) {
						$data = array(
							'periodnumber' => $periodnumber,
							'awardnumbers' => $awardnumbers,
							'awardtime' => $awardtime,
							'game' => 'bj28',
							'addtime' => time()
						);

						$caijinum = M('caiji')->where("game = 'bj28'")->limit(0,1)->order("id desc")->find();
						if (strval($caijinum['periodnumber']) != $periodnumber  && $periodnumber > $caijinum['periodnumber']) {
							//控制开奖
							$kongzhi = M('kongzhi')->where("periodnumber = {$periodnumber} and game='bj28'")->limit(0,1)->order("id desc")->find();
							if ($kongzhi) {
								$data['awardnumbers'] = $kongzhi['awardnumbers'];
							}
						
								
								M('caiji')->add($data);
							
						}
					}		
	    	});
		}
		
		//PC蛋蛋北京28采集
		if (C('jnd28_kj_select') == 1) {
			//xyft开奖采集
			\Workerman\Lib\Timer::add(60, function(){
				$time = time();
					$url = $this->apihost . "xy28sys/3fc.php";
					//$url = 'http://182.61.40.81:810/json/index?ticketname=jnd28sf';
		$josn2 = http_get($url);
		$fdata = json_decode($josn2,true);
		//$fdata = json_decode($josn2,true);
		$fdata = $fdata['data'][0];
					//$cr_data = json_decode($cpk,true);
					//foreach ($fdata  as  $v) { 只有一条采集，老头修改 QQ 263384487
						$periodnumber = $fdata['expect'];
						$awardtime = date('Y-m-d H:i:s', strtotime($fdata['opentime']));
						$awardnumbers = $fdata['opencode'];

						if ($periodnumber && $awardnumbers && $awardtime) {
							$data = array(
								'periodnumber' => $periodnumber,
								'awardnumbers' => $awardnumbers,
								'awardtime' => $awardtime,
								'game' => 'jnd28',
								'addtime' => time()
							);

							$caijinum = M('caiji')->where("game = 'jnd28'")->limit(0,1)->order("id desc")->find();
							if (strval($caijinum['periodnumber']) != $periodnumber  && $periodnumber > $caijinum['periodnumber']) {
								//控制开奖
								$kongzhi = M('kongzhi')->where("periodnumber = {$periodnumber} and game='jnd28'")->limit(0,1)->order("id desc")->find();
								if ($kongzhi) {
									$data['awardnumbers'] = $kongzhi['awardnumbers'];
								}
								
									
									M('caiji')->add($data);
								
							}
						}
					//}
	    	});
		} else {
			\Workerman\Lib\Timer::add(60, function(){
				$time = time();
					$url = C('jnd28_api');
					$cpk = http_get($url);
					$cr_data = json_decode($cpk);
					foreach ($cr_data as $key => $value) {
						$periodnumber = $key;
						$awardtime = $value->dateline;

						$number = explode(',',$value->number);

						$one = $number[1]+$number[4]+$number[7]+$number[10]+$number[13]+$number[16]; 
						$two = $number[2]+$number[5]+$number[8]+$number[11]+$number[14]+$number[17]; 
						$three = $number[3]+$number[6]+$number[9]+$number[12]+$number[15]+$number[18]; 

						
						$one_end = substr($one,-1);
						$two_end = substr($two,-1);
						$three_end = substr($three,-1);

						$awardnumbers = "$one_end,$two_end,$three_end";
						break;
					}

					if ($periodnumber && $awardnumbers && $awardtime) {
						$data = array(
							'periodnumber' => $periodnumber,
							'awardnumbers' => $awardnumbers,
							'awardtime' => $awardtime,
							'game' => 'jnd28',
							'addtime' => time()
						);

						$caijinum = M('caiji')->where("game = 'jnd28'")->limit(0,1)->order("id desc")->find();
						if (strval($caijinum['periodnumber']) != $periodnumber  && $periodnumber > $caijinum['periodnumber']) {
							//控制开奖
							$kongzhi = M('kongzhi')->where("periodnumber = {$periodnumber} and game='jnd28'")->limit(0,1)->order("id desc")->find();
							if ($kongzhi) {
								$data['awardnumbers'] = $kongzhi['awardnumbers'];
							}
							
								M('caiji')->add($data);
							
						}
					}		
	    	});
		}

		\Workerman\Lib\Timer::add(60, function(){
			//开启自动返水
			if (C('is_auto_fs') == '1') {
				if (time() == strtotime('06:00:01')) {
					// $this->update_config(array('is_open'=>0));
					if (C('who_fs') == '1') {
						//自动返水
						$agents = M('user')->where("status = 1 and is_agent = 1")->select();

						foreach ($agents as $a => $b) {
							$time = date('Y-m-d',mktime(0,0,0,date('m'),date('d')-1,date('Y')));
						
							$aid = $b['id'];
							//查询当天是否已返水
							$is_fs = M('fs_date')->where("fs_date='{$time}' and a_id = {$aid}")->find();
							if ($is_fs) {
								continue;
							}

							//查询当天所有的订单,以人分组
							$start_time = strtotime($time.' 00:00:00');
							$end_time = $start_time + 24*3600;
							
							$map['time'] = array(array('egt',$start_time),array('elt',$end_time),'and');
							$map['state'] = 1;
							$map['t_id'] = $aid;
							$order = M("order");
							$list = $order->field("userid,nickname,SUM(add_points) AS add_points,SUM(del_points) AS del_points")->where($map)->group('userid')->select();
							$fs_date = array(
								'fs_date' => $time,
								'add_time' => time()
							);
							$fs_date['water'] = 0;
							$fs_water['pkft'] = 0;
							$fs_water['ssc'] = 0;
							$fs_water['k3'] = 0;
							$fs_water['pcdd'] = 0;

							$t_userinfo = M('user')->field('pkft_fs,ssc_fs,pcdd_fs,k3_fs,yong')->where("id = {$aid}")->find();
							foreach ($list as $key => $value) {
								$fs_date['water'] += $value['del_points'];
								if ($value['game'] == 'pk10' || $value['game'] == 'xyft') {
									$fs_water['pkft'] += $value['del_points'] * $t_userinfo['pkft_fs'] *0.01;
								} else if($value['game'] == 'ssc') {
									$fs_water['ssc'] += $value['del_points'] * $t_userinfo['ssc_fs'] *0.01;
								} else if($value['game'] == 'k3') {
									$fs_water['k3'] += $value['del_points'] * $t_userinfo['k3_fs'] *0.01;
								}
								else {
									$fs_water['pcdd'] += $value['del_points'] * $t_userinfo['pcdd_fs'] *0.01;
								}
							}

							$fs_date['fs_money'] = $fs_water['pkft'] + $fs_water['ssc'] + $fs_water['k3'] + $fs_water['pcdd'] ;

							$fs_date['a_id'] = $aid;

							//是否代理返水，从代理的佣金中扣
							$yong_res = M('user')->where("id = {$aid}")->setDec("yong",$fs_date['fs_money']);
							$zero_res = M('user')->where("id = {$aid}")->save(array('yong'=>0));
							$fs_res = M('fs_date')->add($fs_date);

							foreach ($list as $key => $value) {
								$list[$key]['fs_id'] = $fs_res;
								$list[$key]['date'] = $time;
								$list[$key]['water'] = $value['del_points'];

								if ($value['game'] == 'pk10' || $value['game'] == 'xyft') {
									$list[$key]['money'] = $value['del_points']  * $t_userinfo['pkft_fs'] *0.01;
								} 
								else if($value['game'] == 'ssc') {
									$list[$key]['money'] = $value['del_points']  * $t_userinfo['ssc_fs'] *0.01;
								}  
								else if($value['game'] == 'k3') {
									$list[$key]['money'] = $value['del_points']  * $t_userinfo['k3_fs'] *0.01;
								}
								else {
									$list[$key]['money'] = $value['del_points']  * $t_userinfo['pcdd_fs'] *0.01;
								}
								//给会员凡事操作
								$res = M('user')->where("id={$value['userid']}")->setInc('fanshui', $list[$key]['money']);
							}

							$fs_detail_res = M('fs_details')->addAll($list);

						}
					} else {
						$time = date('Y-m-d',mktime(0,0,0,date('m'),date('d')-1,date('Y')));
						
						//查询当天是否已返水
						$is_fs = M('fs_date')->where("fs_date='{$time}'")->find();
						if ($is_fs) {
							return;
						}

						//查询当天所有的订单,以人分组
						$start_time = strtotime($time.' 00:00:00');
						$end_time = $start_time + 24*3600;

						$map['time'] = array(array('egt',$start_time),array('elt',$end_time),'and');
						$map['state'] = 1;

						if (C('is_dxds_fs') == '0') {
							$map['type'] = array('neq','1');
						}

						$order = M("order");
						$list = $order->field("userid as uid,nickname,SUM(add_points) AS add_points,SUM(del_points) AS del_points,game")->where($map)->group('userid')->select();

						$fs_date = array(
							'fs_date' => $time,
							'add_time' => time()
						);
						$fs_date['water'] = 0;
			$fs_water['bj28_1'] = 0;
			$fs_water['bj28_2'] = 0;
			$fs_water['bj28_3'] = 0;
			$fs_water['jnd28_1'] = 0;
			$fs_water['jnd28_2'] = 0;
			$fs_water['jnd28_3'] = 0;

			foreach ($list as $key => $value) {
				$fs_date['water'] += $value['del_points'];
				if ($value['game'] == 'bj28_1') {
					$fs_water['bj28_1'] += $value['del_points'] * C('bj28_1_fs_rate') *0.01;
				} else if($value['game'] == 'bj28_2') {
					$fs_water['bj28_2'] += $value['del_points'] * C('bj28_2_fs_rate') *0.01;
				} else if($value['game'] == 'bj28_3') {
					$fs_water['bj28_3'] += $value['del_points'] * C('bj28_3_fs_rate') *0.01;
				} else if($value['game'] == 'jnd28_1') {
					$fs_water['jnd28_1'] += $value['del_points'] * C('jnd28_1_fs_rate') *0.01;
				} else if($value['game'] == 'jnd28_2') {
					$fs_water['jnd28_2'] += $value['del_points'] * C('jnd28_2_fs_rate') *0.01;
				} else if($value['game'] == 'jnd28_3') {
					$fs_water['jnd28_3'] += $value['del_points'] * C('jnd28_3_fs_rate') *0.01;
				} 
			}

			$fs_date['fs_money'] = $fs_water['bj28_1'] + $fs_water['bj28_2'] + $fs_water['bj28_3'] + $fs_water['jnd28_1'] + $fs_water['jnd28_2'] + $fs_water['jnd28_3'] ;


			$fs_res = M('fs_date')->add($fs_date);

			foreach ($list as $key => $value) {
				$list[$key]['fs_id'] = $fs_res;
				$list[$key]['date'] = $time;
				$list[$key]['water'] = $value['del_points'];

				if ($value['game'] == 'bj28_1') {
					$list[$key]['money'] = $value['del_points'] * C('bj28_1_fs_rate') *0.01;
				} else if($value['game'] == 'bj28_2') {
					$list[$key]['money'] = $value['del_points'] * C('bj28_2_fs_rate') *0.01;
				} else if($value['game'] == 'bj28_3') {
					$list[$key]['money'] = $value['del_points'] * C('bj28_3_fs_rate') *0.01;
				} else if($value['game'] == 'jnd28_1') {
					$list[$key]['money'] =  $value['del_points'] * C('jnd28_1_fs_rate') *0.01;
				} else if($value['game'] == 'jnd28_2') {
					$list[$key]['money'] =  $value['del_points'] * C('jnd28_2_fs_rate') *0.01;
				} else if($value['game'] == 'jnd28_3') {
					$list[$key]['money'] =  $value['del_points'] * C('jnd28_3_fs_rate') *0.01;
				} else if($value['game'] == 'jnd28_4') {
					$list[$key]['money'] =  $value['del_points'] * C('jnd28_4_fs_rate') *0.01;
				} 
							
							//给会员凡事操作
							$res = M('user')->where("id={$value['uid']}")->setInc('fanshui', $list[$key]['money']);
						}

						$fs_detail_res = M('fs_details')->addAll($list);
					}
				}
			}

			if (time() == strtotime('08:00:00')) {
				// $this->update_config(array('is_open'=>1));
			}
		});
	}
	
	/*
	 * 客户端连接时
	 * 
	 * */
	public function onConnect($connection){
		$connection->onWebSocketConnect = function($connection , $http_header)
		{
        
		};
	}
	
	/**
	 * onMessage
	 * @access public
	 * 转发客户端消息
	 * @param  void
	 * @param  void
	 * @return void
	 */
	public function onMessage($connection, $data) {
		
	}
	
	/**
	 * onClose
	 * 关闭连接
	 * @access public
	 * @param  void
	 * @return void
	 */
	public function onClose($connection) {
		
	}

	public function update_config($post) {
		$filename = 'site.php';
		$config_file = CONF_PATH . $filename;
		if (is_file($config_file) && is_writable($config_file)) {

			$config =
			require $config_file;

			$config = array_merge($config, $post);

			file_put_contents($config_file, "<?php \nreturn " . stripslashes(var_export($config, true)) . ";", LOCK_EX);

			@unlink(RUNTIME_FILE);

			return true;

		} else {

			return false;

		}

	}
}
?>